package com.bootdo.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.mq.RabbitUtil;
import com.bootdo.order.dao.OrderDao;
import com.bootdo.order.dao.OrderPayDao;
import com.bootdo.order.domain.OrderDO;
import com.bootdo.order.domain.OrderPayDO;
import com.bootdo.quota.dao.QuotaDao;
import com.bootdo.quota.domain.QuotaDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.order.dao.RecoverDao;
import com.bootdo.order.domain.RecoverDO;
import com.bootdo.order.service.RecoverService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class RecoverServiceImpl implements RecoverService {
	@Autowired
	private RecoverDao recoverMapper;
	@Autowired
	private OrderPayDao orderPayMapper;
	@Autowired
	private OrderDao orderMapper;
	@Autowired
	private QuotaDao quotaMapper;
	@Autowired
	private RabbitUtil rabbitUtil;
	
	@Override
	public RecoverDO get(Long id){
		return recoverMapper.get(id);
	}
	
	@Override
	public List<RecoverDO> list(Map<String, Object> map){
		return recoverMapper.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return recoverMapper.count(map);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
	public int save(RecoverDO recover){
		return recoverMapper.save(recover);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
	public int update(RecoverDO recover){
		int status = recover.getStatus();
		recover = recoverMapper.get(recover.getId());
		if (1 == recover.getStatus().intValue()){
			recover.setStatus(status);
			if (status == 2) {
				OrderPayDO orderPay = orderPayMapper.get(recover.getOrderId());
				//审核通过，恢复收款户额度
				JSONObject object = new JSONObject();
				object.put("date", DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN));
				object.put("userId", orderPay.getPayeeId());
				object.put("money", orderPay.getPayeeTurn());
				object.put("orderId", orderPay.getOrderId());
				object.put("reason", "订单被追回");
				object.put("type", 1);
				rabbitUtil.sendToQueue(Constant.UPDATE_QUOTA, object.toJSONString());

				//恢复收款户代理额度
				QuotaDO quota = quotaMapper.get(orderPay.getPayeeAgentId());
				if (quota != null) {
					object.put("userId", orderPay.getPayeeAgentId());
					object.put("money", orderPay.getPayeeAgentTurn());
					rabbitUtil.sendToQueue(Constant.UPDATE_QUOTA, object.toJSONString());
				}
				//扣除商户余额
				object.put("userId", orderPay.getMerchantId());
				object.put("money", orderPay.getOrderMoney() - orderPay.getCountMoney());
				object.put("type", 2);
				rabbitUtil.sendToQueue(Constant.UPDATE_QUOTA, object.toJSONString());

				//减去商户代理
				quota = quotaMapper.get(orderPay.getMerchantAgentId());
				if (quota != null) {
					object.put("userId", orderPay.getMerchantAgentId());
					object.put("money", (orderPay.getMerchantMoney() == null ? 0.0 : orderPay.getMerchantMoney()));
					rabbitUtil.sendToQueue(Constant.UPDATE_QUOTA, object.toJSONString());
				}

				//扣除平台流水
				object.put("userId", 2L);
				object.put("money", orderPay.getPlatformMoney());
				rabbitUtil.sendToQueue(Constant.UPDATE_QUOTA, object.toJSONString());

				//将订单状态修改为已失效
				orderPay.setStatus(4);
				orderPayMapper.update(orderPay);
				OrderDO order = orderMapper.get(orderPay.getOrderId());
				order.setType(4);
				orderMapper.update(order);
			}
		}
		return recoverMapper.update(recover);
	}
	
}
